package cn.humaohua.study.c6.v2;

import cn.humaohua.study.c6.MaxTemperatureMapper;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 *  cd
 *  mvn compile
 *  export HADOOP_CLASSPATH=target/classes/
 *  hadoop cn.humaohua.study.c6.v2.MaxTemperatureDriver -conf conf/hadoop-local.xml hdfs://localhost/user/hmh/input/ncdc/micro output
 *  如果报下面异常：
 *  Exception in thread "main" org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory file:/Users/hmh/Documents/docs/source_code/git/hadoop-mr/output already exists
 *  需要用下面命令删除输出文件夹：
 *  rm -rf /Users/hmh/Documents/docs/source_code/git/hadoop-mr/output
 *
 *  cat output/part-r-00000
 *
 *  hadoop fs -ls . -conf conf/hadoop-local.xml
 *
 *
 */
public class MaxTemperatureDriver extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.printf("Usage: %s [generic options] <input> <output>\n",
                    getClass().getSimpleName());
            ToolRunner.printGenericCommandUsage(System.err);
            return -1;
        }
        Job job = new Job(getConf(), "Max temperature");
        job.setJarByClass(getClass());
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.setMapperClass(MaxTemperatureMapper.class);
        job.setCombinerClass(MaxTemperatureReducer.class);
        job.setReducerClass(MaxTemperatureReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        int exitCode = ToolRunner.run(new MaxTemperatureDriver(), args);
        System.exit(exitCode);
    }
}
